package com.alasbusy.alasbusyapp.activity.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.activity.LoginActivity;
import com.alasbusy.alasbusyapp.activity.pic.SelfInfoActivity;
import com.alasbusy.alasbusyapp.activity.twoactivity.AboutMineActivity;
import com.alasbusy.alasbusyapp.base.BaseFragment;
import com.alasbusy.alasbusyapp.utils.CircleImageView;
import com.alasbusy.alasbusyapp.utils.SharedPreferencesHelp;
import com.bumptech.glide.Glide;
import com.zhy.android.percent.support.PercentLinearLayout;

/**
 * Created by Administrator on 2018/5/14.
 */

public class MySelfFragment extends BaseFragment implements View.OnClickListener {

    private CircleImageView my_img;
    private TextView my_name;
    private ImageView my_info;
    private boolean loginSuccess;
    private ImageView my_about;

    @Override
    public int getLayout() {
        return R.layout.myself_fragment;
    }

    @Override
    protected void initView(View view) {
        PercentLinearLayout myself_fragment_title = view.findViewById(R.id.myself_fragment_title);
        setTop(myself_fragment_title);
        getVisibility(false,getActivity());
        getTitle("我的");

        my_img = view.findViewById(R.id.my_img);
        my_name = view.findViewById(R.id.my_name);
        my_info = view.findViewById(R.id.my_info);
        my_about = view.findViewById(R.id.my_about);

        loginSuccess = SharedPreferencesHelp.getInstance().getLoginSuccess();

        if(!TextUtils.isEmpty(SharedPreferencesHelp.getInstance().getUserHeader())) {
            /*Bitmap bitmap = BitmapFactory.decodeFile(SharedPreferencesHelp.getInstance().getUserHeader());
            header_image.setImageBitmap(bitmap);*/
            Glide.with(this)
                    .load(SharedPreferencesHelp.getInstance().getUserHeader())
                    //.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸
                    .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                    //.error(R.drawable.my_normal)
                    .into(/*new GlideDrawableImageViewTarget(*/my_img) /*{
                        @Override
                        public void onLoadStarted(Drawable placeholder) {
                            super.onLoadStarted(placeholder);
                        }

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            super.onLoadFailed(e, errorDrawable);
                        }

                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                            super.onResourceReady(resource, animation);
                        }
                    })*/;
        }else {
            my_img.setImageResource(R.drawable.my_normal);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        my_img.setOnClickListener(this);
        my_name.setOnClickListener(this);
        my_info.setOnClickListener(this);
        my_about.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_img:
            case R.id.my_name:
                if(loginSuccess) {
                    getActivity().startActivity(new Intent(getContext(), SelfInfoActivity.class));
                }else {
                    getActivity().startActivity(new Intent(getContext(), LoginActivity.class));
                }
                break;
            case R.id.my_info:
                if(loginSuccess) {
                    getActivity().startActivity(new Intent(getContext(), SelfInfoActivity.class));
                }else {
                    getActivity().startActivity(new Intent(getContext(), LoginActivity.class));
                }
                break;
            case R.id.my_about:
                startActivity(new Intent(getActivity(), AboutMineActivity.class));
                break;
        }
    }
}
